home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / itc / cc1.spur / global-alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  27.6 KB  |  906 lines

  1. /* Allocate registers for pseudo-registers that span basic blocks.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include "config.h"
  24. #include "rtl.h"
  25. #include "flags.h"
  26. #include "basic-block.h"
  27. #include "hard-reg-set.h"
  28. #include "regs.h"
  29. #include "insn-config.h"
  30. #include "alloca.h"
  31.  
  32. /* This pass of the compiler performs global register allocation.
  33.    It assigns hard register numbers to all the pseudo registers
  34.    that were not handled in local_alloc.  Assignments are recorded
  35.    in the vector reg_renumber, not by changing the rtl code.
  36.    (Such changes are made by final).  The entry point is
  37.    the function global_alloc.
  38.  
  39.    After allocation is complete, the reload pass is run as a subroutine
  40.    of this pass, so that when a pseudo reg loses its hard reg due to
  41.    spilling it is possible to make a second attempt to find a hard
  42.    reg for it.  The reload pass is independent in other respects
  43.    and it is run even when stupid register allocation is in use.
  44.  
  45.    1. count the pseudo-registers still needing allocation
  46.    and assign allocation-numbers (allocnos) to them.
  47.    Set up tables reg_allocno and allocno_reg to map 
  48.    reg numbers to allocnos and vice versa.
  49.    max_allocno gets the number of allocnos in use.
  50.  
  51.    2. Allocate a max_allocno by max_allocno conflict bit matrix and clear it.
  52.    Allocate a max_allocno by FIRST_PSEUDO_REGISTER conflict matrix
  53.    for conflicts between allocnos and explicit hard register use
  54.    (which includes use of pseudo-registers allocated by local_alloc).
  55.  
  56.    3. for each basic block
  57.     walk forward through the block, recording which
  58.     unallocated registers and which hardware registers are live.
  59.     Build the conflict matrix between the unallocated registers
  60.     and another of unallocated registers versus hardware registers.
  61.     Someday also record the preferred hardware registers
  62.     for each unallocated one.
  63.  
  64.    4. Sort a table of the allocnos into order of
  65.    desirability of the variables.
  66.  
  67.    5. Allocate the variables in that order; each if possible into
  68.    an preferred register, else into another register.  */
  69.  
  70. /* Number of pseudo-registers still requiring allocation
  71.    (not allocated by local_allocate).  */
  72.  
  73. static int max_allocno;
  74.  
  75. /* Indexed by (pseudo) reg number, gives the allocno, or -1
  76.    for pseudo registers already allocated by local_allocate.  */
  77.  
  78. static int *reg_allocno;
  79.  
  80. /* Indexed by allocno, gives the reg number.  */
  81.  
  82. static int *allocno_reg;
  83.  
  84. /* A vector of the integers from 0 to max_allocno-1,
  85.    sorted in the order of first-to-be-allocated first.  */
  86.  
  87. static int *allocno_order;
  88.  
  89. /* Indexed by an allocno, gives the number of consecutive
  90.    hard registers needed by that pseudo reg.  */
  91.  
  92. static int *allocno_size;
  93.  
  94. /* Indexed by allocno, gives number of preferred hard reg, or -1 if none.  */
  95.  
  96. static int *allocno_preferred_reg;
  97.  
  98. /* max_allocno by max_allocno array of bits,
  99.    recording whether two allocno's conflict (can't go in the same
  100.    hardware register).
  101.  
  102.    `conflicts' is not symmetric; a conflict between allocno's i and j
  103.    is recorded either in element i,j or in element j,i.  */
  104.  
  105. static int *conflicts;
  106.  
  107. /* Number of ints require to hold max_allocno bits.
  108.    This is the length of a row in `conflicts'.  */
  109.  
  110. static int allocno_row_words;
  111.  
  112. /* Two macros to test or store 1 in an element of `conflicts'.  */
  113.  
  114. #define CONFLICTP(I, J) \
  115.  (conflicts[(I) * allocno_row_words + (J) / INT_BITS]    \
  116.   & (1 << ((J) % INT_BITS)))
  117.  
  118. #define SET_CONFLICT(I, J) \
  119.  (conflicts[(I) * allocno_row_words + (J) / INT_BITS]    \
  120.   |= (1 << ((J) % INT_BITS)))
  121.  
  122. /* Set of hard regs currently live (during scan of all insns).  */
  123.  
  124. static HARD_REG_SET hard_regs_live;
  125.  
  126. /* Indexed by N, set of hard regs conflicting with allocno N.  */
  127.  
  128. static HARD_REG_SET *hard_reg_conflicts;
  129.  
  130. #if 0
  131. /* Indexed by N, set of hard regs preferred by allocno N.
  132.    This was intended to be used to make allocnos go into regs
  133.    that they are loaded from, when possible, to reduce register shuffling.  */
  134.  
  135. static HARD_REG_SET *hard_reg_preferences;
  136. #endif
  137.  
  138. /* Set of registers that global-alloc isn't supposed to use.  */
  139.  
  140. static HARD_REG_SET no_global_alloc_regs;
  141.  
  142. /* Test a bit in TABLE, a vector of HARD_REG_SETs,
  143.    for vector element I, and hard register number J.  */
  144.  
  145. #define REGBITP(TABLE, I, J)     TEST_HARD_REG_BIT (TABLE[I], J)
  146.  
  147. /* Set to 1 a bit in a vector of HARD_REG_SETs.  Works like REGBITP.  */
  148.  
  149. #define SET_REGBIT(TABLE, I, J)  SET_HARD_REG_BIT (TABLE[I], J)
  150.  
  151. /* Bit mask for allocnos live at current point in the scan.  */
  152.  
  153. static int *allocnos_live;
  154.  
  155. #define INT_BITS HOST_BITS_PER_INT
  156.  
  157. /* Test, set or clear bit number I in allocnos_live,
  158.    a bit vector indexed by allocno.  */
  159.  
  160. #define ALLOCNO_LIVE_P(I) \
  161.   (allocnos_live[(I) / INT_BITS] & (1 << ((I) % INT_BITS)))
  162.  
  163. #define SET_ALLOCNO_LIVE(I) \
  164.   (allocnos_live[(I) / INT_BITS] |= (1 << ((I) % INT_BITS)))
  165.  
  166. #define CLEAR_ALLOCNO_LIVE(I) \
  167.   (allocnos_live[(I) / INT_BITS] &= ~(1 << ((I) % INT_BITS)))
  168.  
  169. static int allocno_compare ();
  170. static void mark_reg_store ();
  171. static void mark_reg_live_nc ();
  172. static void mark_reg_death ();
  173. static void dump_conflicts ();
  174. static void find_reg ();
  175. static void global_conflicts ();
  176. static void record_conflicts ();
  177.  
  178.  
  179. /* Tables describing and classifying the hardware registers.  */
  180.  
  181. /* Perform allocation of pseudo-registers not allocated by local_alloc.
  182.    FILE is a file to output debugging information on,
  183.    or zero if such output is not desired.  */
  184.  
  185. void
  186. global_alloc (file)
  187.      FILE *file;
  188. {
  189.   register int i;
  190.  
  191.   max_allocno = 0;
  192.  
  193.   /* A machine may have certain hard registers that
  194.      are safe to use only within a basic block.  */
  195.  
  196.   CLEAR_HARD_REG_SET (no_global_alloc_regs);
  197. #ifdef OVERLAPPING_REGNO_P
  198.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  199.     if (OVERLAPPING_REGNO_P (i))
  200.       SET_HARD_REG_BIT (no_global_alloc_regs, i);
  201. #endif
  202.  
  203.   /* Establish mappings from register number to allocation number
  204.      and vice versa.  In the process, count the allocnos.  */
  205.  
  206.   reg_allocno = (int *) alloca (max_regno * sizeof (int));
  207.  
  208.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  209.     reg_allocno[i] = -1;
  210.  
  211.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  212.     /* Note that reg_live_length[i] < 0 indicates a "constant" reg
  213.        that we are supposed to refrain from putting in a hard reg.
  214.        -2 means do make an allocno but don't allocate it.  */
  215.     if (reg_n_refs[i] != 0 && reg_renumber[i] < 0 && reg_live_length[i] != -1)
  216.       {
  217.     reg_allocno[i] = max_allocno++;
  218.     if (reg_live_length[i] == 0)
  219.       abort ();
  220.       }
  221.     else
  222.       reg_allocno[i] = -1;
  223.  
  224.   allocno_reg = (int *) alloca (max_allocno * sizeof (int));
  225.   allocno_size = (int *) alloca (max_allocno * sizeof (int));
  226.   allocno_preferred_reg = (int *) alloca (max_allocno * sizeof (int));
  227.   bzero (allocno_size, max_allocno * sizeof (int));
  228.  
  229.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  230.     if (reg_allocno[i] >= 0)
  231.       {
  232.     allocno_reg[reg_allocno[i]] = i;
  233.     allocno_size[reg_allocno[i]] = PSEUDO_REGNO_SIZE (i);
  234.     allocno_preferred_reg[reg_allocno[i]] = -1;
  235.       }
  236.  
  237.   /* Allocate the space for the conflict tables.  */
  238.  
  239.   hard_reg_conflicts = (HARD_REG_SET *)
  240.     alloca (max_allocno * sizeof (HARD_REG_SET));
  241.   bzero (hard_reg_conflicts, max_allocno * sizeof (HARD_REG_SET));
  242.  
  243. #if 0
  244.   hard_reg_preferences = (HARD_REG_SET *)
  245.     alloca (max_allocno * sizeof (HARD_REG_SET));
  246.   bzero (hard_reg_preferences, max_allocno * sizeof (HARD_REG_SET));
  247. #endif
  248.  
  249.   allocno_row_words = (max_allocno + INT_BITS - 1) / INT_BITS;
  250.  
  251.   conflicts = (int *)
  252.     alloca (max_allocno * allocno_row_words * sizeof (int));
  253.   bzero (conflicts, max_allocno * allocno_row_words * sizeof (int));
  254.  
  255.   allocnos_live = (int *) alloca (allocno_row_words * sizeof (int));
  256.  
  257.   /* If there is work to be done (at least one reg to allocate),
  258.      perform global conflict analysis and allocate the regs.  */
  259.  
  260.   if (max_allocno > 0)
  261.     {
  262.       /* Scan all the insns and compute the conflicts among allocnos
  263.      and between allocnos and hard regs.  */
  264.  
  265.       global_conflicts ();
  266.  
  267.       /* Determine the order to allocate the remaining pseudo registers.  */
  268.  
  269.       allocno_order = (int *) alloca (max_allocno * sizeof (int));
  270.       for (i = 0; i < max_allocno; i++)
  271.     allocno_order[i] = i;
  272.  
  273.       /* Default the size to 1, since allocno_compare uses it to divide by.  */
  274.  
  275.       for (i = 0; i < max_allocno; i++)
  276.     if (allocno_size[i] == 0)
  277.       allocno_size[i] = 1;
  278.  
  279.       qsort (allocno_order, max_allocno, sizeof (int), allocno_compare);
  280.  
  281.       if (file)
  282.     dump_conflicts (file);
  283.  
  284.       /* Try allocating them, one by one, in that order,
  285.      except for parameters marked with reg_live_length[regno] == -2.  */
  286.  
  287.       for (i = 0; i < max_allocno; i++)
  288.     if (reg_live_length[allocno_reg[allocno_order[i]]] >= 0)
  289.       {
  290.         /* If we have a preferred hard register, try that first.  */
  291.         if (allocno_preferred_reg[allocno_order[i]] >= 0)
  292.           {
  293.         find_reg (allocno_order[i], 0, 0,
  294.               allocno_preferred_reg[allocno_order[i]]);
  295.         if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
  296.           continue;
  297.           }
  298.         /* If we have more than one register class,
  299.            first try allocating in the class that is cheapest
  300.            for this pseudo-reg.  If that fails, try any reg.  */
  301.         if (N_REG_CLASSES > 1)
  302.           {
  303.         find_reg (allocno_order[i], 0, 0, -1);
  304.         if (reg_renumber[allocno_reg[allocno_order[i]]] >= 0)
  305.           continue;
  306.           }
  307.         if (!reg_preferred_or_nothing (allocno_reg[allocno_order[i]]))
  308.           find_reg (allocno_order[i], 0, 1, -1);
  309.       }
  310.     }
  311.  
  312.   /* Do the reloads now while the allocno data still exist, so that we can
  313.      try to assign new hard regs to any pseudo regs that are spilled.  */
  314.  
  315.   if (n_basic_blocks > 0)
  316.     reload (basic_block_head[0], 1, file);
  317. }
  318.  
  319. /* Sort predicate for ordering the allocnos.
  320.    Returns -1 (1) if *v1 should be allocated before (after) *v2.  */
  321.  
  322. static int
  323. allocno_compare (v1, v2)
  324.      int *v1, *v2;
  325. {
  326.   register int r1 = allocno_reg[*v1];
  327.   register int r2 = allocno_reg[*v2];
  328.   /* Note that the quotient will never be bigger than
  329.      the value of floor_log2 times the maximum number of
  330.      times a register can occur in one insn (surely less than 100).
  331.      Multiplying this by 10000 can't overflow.  */
  332.   register int pri1
  333.     = (((double) (floor_log2 (reg_n_refs[r1]) * reg_n_refs[r1])
  334.     / (reg_live_length[r1] * allocno_size[*v1]))
  335.        * 10000);
  336.   register int pri2
  337.     = (((double) (floor_log2 (reg_n_refs[r2]) * reg_n_refs[r2])
  338.     / (reg_live_length[r2] * allocno_size[*v2]))
  339.        * 10000);
  340.   return pri2 - pri1;
  341. }
  342.  
  343. /* Scan the rtl code and record all conflicts in the conflict matrices.  */
  344.  
  345. static void
  346. global_conflicts ()
  347. {
  348.   register int b, i;
  349.   register rtx insn;
  350.   short *block_start_allocnos;
  351.  
  352.   block_start_allocnos = (short *) alloca (max_allocno * sizeof (short));
  353.  
  354.   for (b = 0; b < n_basic_blocks; b++)
  355.     {
  356.       bzero (allocnos_live, allocno_row_words * sizeof (int));
  357.  
  358.       /* Initialize table of registers currently live
  359.      to the state at the beginning of this basic block.
  360.      This also marks the conflicts among them.
  361.  
  362.      For pseudo-regs, there is only one bit for each one
  363.      no matter how many hard regs it occupies.
  364.      This is ok; we know the size from PSEUDO_REGNO_SIZE.
  365.      For explicit hard regs, we cannot know the size that way
  366.      since one hard reg can be used with various sizes.
  367.      Therefore, we must require that all the hard regs
  368.      implicitly live as part of a multi-word hard reg
  369.      are explicitly marked in basic_block_live_at_start.  */
  370.  
  371.       {
  372.     register int offset, bit;
  373.     register regset old = basic_block_live_at_start[b];
  374.     int ax = 0;
  375.  
  376. #ifdef HARD_REG_SET
  377.     hard_regs_live = old[0];
  378. #else
  379.     COPY_HARD_REG_SET (hard_regs_live, old);
  380. #endif
  381.     for (offset = 0, i = 0; offset < regset_size; offset++)
  382.       if (old[offset] == 0)
  383.         i += HOST_BITS_PER_INT;
  384.       else
  385.         for (bit = 1; bit; bit <<= 1, i++)
  386.           {
  387.         if (i >= max_regno)
  388.           break;
  389.         if (old[offset] & bit)
  390.           {
  391.             register int a = reg_allocno[i];
  392.             if (a >= 0)
  393.               {
  394.             SET_ALLOCNO_LIVE (a);
  395.             block_start_allocnos[ax++] = a;
  396.               }
  397.             else if ((a = reg_renumber[i]) >= 0)
  398.               mark_reg_live_nc (a, PSEUDO_REGNO_MODE (i));
  399.           }
  400.           }
  401.  
  402.     /* Record that each allocno now live conflicts with each other
  403.        allocno now live, and with each hard reg now live.  */
  404.  
  405.     record_conflicts (block_start_allocnos, ax);
  406.       }
  407.  
  408.       insn = basic_block_head[b];
  409.  
  410.       /* Scan the code of this basic block, noting which allocnos
  411.      and hard regs are born or die.  When one is born,
  412.      record a conflict with all others currently live.  */
  413.  
  414.       while (1)
  415.     {
  416.       register RTX_CODE code = GET_CODE (insn);
  417.       register rtx link;
  418.       rtx regs_set[MAX_SETS_PER_INSN + MAX_CLOBBERS_PER_INSN];
  419.       int n_regs_set = 0;
  420.  
  421.       if (code == INSN || code == CALL_INSN || code == JUMP_INSN)
  422.         {
  423.           /* Mark any registers dead after INSN as dead now.  */
  424.  
  425.           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  426.         if (REG_NOTE_KIND (link) == REG_DEAD)
  427.           mark_reg_death (XEXP (link, 0));
  428.  
  429.           /* Mark any registers set in INSN as live,
  430.          and mark them as conflicting with all other live regs.  */
  431.  
  432.           if ((GET_CODE (PATTERN (insn)) == SET
  433.            || GET_CODE (PATTERN (insn)) == CLOBBER)
  434.           && GET_CODE (SET_DEST (PATTERN (insn))) == REG)
  435.         {
  436.           register rtx z = SET_DEST (PATTERN (insn));
  437.           regs_set[n_regs_set++] = z;
  438.           mark_reg_store (z);
  439.         }
  440.           if ((GET_CODE (PATTERN (insn)) == SET
  441.            || GET_CODE (PATTERN (insn)) == CLOBBER)
  442.           && GET_CODE (SET_DEST (PATTERN (insn))) == SUBREG)
  443.         {
  444.           register rtx z = SUBREG_REG (SET_DEST (PATTERN (insn)));
  445.           if (GET_CODE (z) == REG)
  446.             {
  447.               regs_set[n_regs_set++] = z;
  448.               mark_reg_store (z);
  449.             }
  450.         }
  451.           else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  452.         {
  453.           register rtx y = PATTERN (insn);
  454.           for (i = XVECLEN (y, 0) - 1;
  455.                i >= 0; i--)
  456.             if (GET_CODE (XVECEXP (y, 0, i)) == SET
  457.             || GET_CODE (XVECEXP (y, 0, i)) == CLOBBER)
  458.               {
  459.             rtx z = SET_DEST (XVECEXP (y, 0, i));
  460.             if (GET_CODE (z) == REG)
  461.               {
  462.                 regs_set[n_regs_set++] = z;
  463.                 mark_reg_store (z);
  464.               }
  465.             if (GET_CODE (z) == SUBREG
  466.                 && GET_CODE (SUBREG_REG (z)) == REG)
  467.               {
  468.                 regs_set[n_regs_set++] = SUBREG_REG (z);
  469.                 mark_reg_store (SUBREG_REG (z));
  470.               }
  471.               }
  472.         }
  473.  
  474.           /* Mark any registers both set and dead after INSN as dead.
  475.          This is not redundant!
  476.          A register may be set and killed in the same insn.
  477.          It is necessary to mark them as live, above, to get
  478.          the right conflicts within the insn.  */
  479.  
  480.           while (n_regs_set > 0)
  481.         if (find_regno_note (insn, REG_DEAD, REGNO (regs_set[--n_regs_set])))
  482.           mark_reg_death (regs_set[n_regs_set]);
  483.         
  484.           /* Likewise for regs set by incrementation.  */
  485.  
  486.           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  487.         if (REG_NOTE_KIND (link) == REG_INC
  488.             && find_regno_note (insn, REG_DEAD, REGNO (XEXP (link, 0))))
  489.           mark_reg_death (XEXP (link, 0));
  490.         }
  491.  
  492.       if (insn == basic_block_end[b])
  493.         break;
  494.       insn = NEXT_INSN (insn);
  495.     }
  496.     }
  497. }
  498.  
  499. /* Assign a hard register to ALLOCNO; look for one that is the beginning
  500.    of a long enough stretch of hard regs none of which conflicts with ALLOCNO.
  501.    If PREFREG is >= 0, try that hard reg first.
  502.  
  503.    If ALL_REGS_P is zero, consider only the preferred class of ALLOCNO's reg.
  504.    Otherwise ignore that preferred class.
  505.  
  506.    If we find one, record it in reg_renumber.
  507.    If not, do nothing.  */
  508.  
  509. static void
  510. find_reg (allocno, losers, all_regs_p, prefreg)
  511.      int allocno;
  512.      register short *losers;
  513.      int all_regs_p;
  514.      int prefreg;
  515. {
  516.   register int i;
  517. #ifdef HARD_REG_SET
  518.   register        /* Declare it register if it's a scalar.  */
  519. #endif
  520.     HARD_REG_SET used;
  521.  
  522.   enum reg_class class 
  523.     = all_regs_p ? GENERAL_REGS : reg_preferred_class (allocno_reg[allocno]);
  524.   enum machine_mode mode = PSEUDO_REGNO_MODE (allocno_reg[allocno]);
  525.  
  526.   COPY_HARD_REG_SET (used,
  527.              (reg_crosses_call[allocno_reg[allocno]]
  528.               ? call_used_reg_set : fixed_reg_set));
  529.  
  530.   /* Some registers should not be allocated in global-alloc.  */
  531.   IOR_HARD_REG_SET (used, no_global_alloc_regs);
  532.  
  533.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  534.   IOR_HARD_REG_SET (used, hard_reg_conflicts[allocno]);
  535.   if (frame_pointer_needed)
  536.     SET_HARD_REG_BIT (used, FRAME_POINTER_REGNUM);
  537.  
  538.   /* If a preferred register was specified, try it first.  */
  539.  
  540.   i = -1;
  541.   if (prefreg >= 0)
  542.     if (! TEST_HARD_REG_BIT (used, prefreg)
  543.     && (losers == 0 || losers[prefreg] < 0)
  544.     && HARD_REGNO_MODE_OK (prefreg, mode))
  545.       {
  546.     register int j;
  547.     register int lim = prefreg + HARD_REGNO_NREGS (prefreg, mode);
  548.     for (j = prefreg + 1;
  549.          (j < lim
  550.           && ! TEST_HARD_REG_BIT (used, j)
  551.           && (losers == 0 || losers[j] < 0));
  552.          j++);
  553.     if (j == lim)
  554.       i = prefreg;
  555.       }
  556.  
  557.   /* Otherwise try each hard reg to see if it fits.  */
  558.  
  559.   if (i < 0)
  560.     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  561.       {
  562. #ifdef REG_ALLOC_ORDER
  563.     int regno = reg_alloc_order[i];
  564. #else
  565.     int regno = i;
  566. #endif
  567.     if (! TEST_HARD_REG_BIT (used, regno)
  568.         && (losers == 0 || losers[regno] < 0)
  569.         && HARD_REGNO_MODE_OK (regno, mode))
  570.       {
  571.         register int j;
  572.         register int lim = regno + HARD_REGNO_NREGS (regno, mode);
  573.         for (j = regno + 1;
  574.          (j < lim
  575.           && ! TEST_HARD_REG_BIT (used, j)
  576.           && (losers == 0 || losers[j] < 0));
  577.          j++);
  578.         if (j == lim)
  579.           break;
  580. #ifndef REG_ALLOC_ORDER
  581.         regno = j;            /* Skip starting points we know will lose */
  582. #endif
  583.       }
  584.       }
  585.  
  586.   /* Did we find a register?  */
  587.  
  588.   if (i < FIRST_PSEUDO_REGISTER)
  589.     {
  590.       register int lim, j;
  591.       HARD_REG_SET this_reg;
  592.  
  593.       /* Yes.  Record it as the hard register of this pseudo-reg.  */
  594.       reg_renumber[allocno_reg[allocno]] = i;
  595.       /* For each other pseudo-reg conflicting with this one,
  596.      mark it as conflicting with the hard regs this one occupies.  */
  597.       CLEAR_HARD_REG_SET (this_reg);
  598.       lim = i + HARD_REGNO_NREGS (i, mode);
  599.       for (j = i; j < lim; j++)
  600.     SET_HARD_REG_BIT (this_reg, j);
  601.       lim = allocno;
  602.       for (j = 0; j < max_allocno; j++)
  603.     if (CONFLICTP (lim, j) || CONFLICTP (j, lim))
  604.       {
  605.         IOR_HARD_REG_SET (hard_reg_conflicts[j], this_reg);
  606.       }
  607.     }
  608. }
  609.  
  610. /* Called from `reload' to look for a hard reg to put pseudo reg REGNO in.
  611.    Perhaps it had previously seemed not worth a hard reg,
  612.    or perhaps its old hard reg has been commandeered for reloads.
  613.    FORBIDDEN_REGS is a vector that indicates certain hard regs
  614.    that may not be used, even if they do not appear to be allocated.
  615.    A nonnegative element means the corresponding hard reg is forbidden.
  616.    If FORBIDDEN_REGS is zero, no regs are forbidden.  */
  617.  
  618. void
  619. retry_global_alloc (regno, forbidden_regs)
  620.      int regno;
  621.      short *forbidden_regs;
  622. {
  623.   int allocno = reg_allocno[regno];
  624.   if (allocno >= 0)
  625.     {
  626.       /* If we have more than one register class,
  627.      first try allocating in the class that is cheapest
  628.      for this pseudo-reg.  If that fails, try any reg.  */
  629.       if (N_REG_CLASSES > 1)
  630.     find_reg (allocno, forbidden_regs, 0, -1);
  631.       if (reg_renumber[regno] < 0
  632.       && !reg_preferred_or_nothing (regno))
  633.     find_reg (allocno, forbidden_regs, 1, -1);
  634.     }
  635. }
  636.  
  637. /* Called from reload pass to see if current function's pseudo regs
  638.    require a frame pointer to be allocated and set up.
  639.  
  640.    Return 1 if so, 0 otherwise.
  641.    We may alter the hard-reg allocation of the pseudo regs
  642.    in order to make the frame pointer unnecessary.
  643.    However, if the value is 1, nothing has been altered.
  644.  
  645.    Args grant access to some tables used in reload1.c.
  646.    See there for info on them.  */
  647.  
  648. int
  649. check_frame_pointer_required (reg_equiv_constant, reg_equiv_mem)
  650.      rtx *reg_equiv_constant, *reg_equiv_mem;
  651. {
  652.   register int i;
  653.   HARD_REG_SET *old_hard_reg_conflicts;
  654.   short *old_reg_renumber;
  655.   char old_regs_ever_live[FIRST_PSEUDO_REGISTER];
  656.  
  657.   /* If any pseudo reg has no hard reg and no equivalent,
  658.      we must have a frame pointer.  */
  659.  
  660.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  661.     if (reg_renumber[i] < 0 && reg_equiv_mem[i] == 0
  662.     && reg_equiv_constant[i] == 0 && reg_n_refs[i] > 0)
  663.       return 1;
  664.  
  665.   /* If we might not need a frame pointer,
  666.      try finding a hard reg for any pseudo that has a memory equivalent.
  667.      That is because the memory equivalent probably refers to a frame
  668.      pointer.  */
  669.  
  670.   old_reg_renumber = (short *) alloca (max_regno * sizeof (short));
  671.   old_hard_reg_conflicts = (HARD_REG_SET *)
  672.     alloca (max_allocno * sizeof (HARD_REG_SET));
  673.  
  674.   bcopy (reg_renumber, old_reg_renumber, max_regno * sizeof (short));
  675.   bcopy (hard_reg_conflicts, old_hard_reg_conflicts,
  676.      max_allocno * sizeof (HARD_REG_SET));
  677.   bcopy (regs_ever_live, old_regs_ever_live, sizeof regs_ever_live);
  678.  
  679.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  680.     if (reg_renumber[i] < 0 && reg_equiv_mem[i] != 0)
  681.       {
  682.     retry_global_alloc (i, 0);
  683.     /* If we can't find a hard reg for ALL of them,
  684.        or if a previously unneeded hard reg is used that requires saving,
  685.        we fail: set all those pseudos back as they were.  */
  686.     if (reg_renumber[i] < 0
  687.         || (! old_regs_ever_live[reg_renumber[i]]
  688.         && ! call_used_regs[reg_renumber[i]]))
  689.       {
  690.         bcopy (old_reg_renumber, reg_renumber,
  691.            max_regno * sizeof (short));
  692.         bcopy (old_hard_reg_conflicts, hard_reg_conflicts,
  693.            max_allocno * sizeof (HARD_REG_SET));
  694.         bcopy (old_regs_ever_live, regs_ever_live, sizeof regs_ever_live);
  695.         return 1;
  696.       }
  697.     mark_home_live (i);
  698.       }
  699.  
  700.   return 0;
  701. }
  702.  
  703. /* Record a conflict between register REGNO
  704.    and everything currently live.
  705.    REGNO must not be a pseudo reg that was allocated
  706.    by local_alloc; such numbers must be translated through
  707.    reg_renumber before calling here.  */
  708.  
  709. static void
  710. record_one_conflict (regno)
  711.      int regno;
  712. {
  713.   register int j;
  714.  
  715.   if (regno < FIRST_PSEUDO_REGISTER)
  716.     /* When a hard register becomes live,
  717.        record conflicts with live pseudo regs.  */
  718.     for (j = 0; j < max_allocno; j++)
  719.       {
  720.     if (ALLOCNO_LIVE_P (j))
  721.       SET_HARD_REG_BIT (hard_reg_conflicts[j], regno);
  722.       }
  723.   else
  724.     /* When a pseudo-register becomes live,
  725.        record conflicts first with hard regs,
  726.        then with other pseudo regs.  */
  727.     {
  728.       register int ialloc = reg_allocno[regno];
  729.       register int ialloc_prod = ialloc * allocno_row_words;
  730.       IOR_HARD_REG_SET (hard_reg_conflicts[ialloc], hard_regs_live);
  731.       for (j = allocno_row_words - 1; j >= 0; j--)
  732.     conflicts[ialloc_prod + j] |= allocnos_live[j];
  733.     }
  734. }
  735.  
  736. /* Record all allocnos currently live as conflicting
  737.    with each other and with all hard regs currently live.
  738.    ALLOCNO_VEC is a vector of LEN allocnos, all allocnos that
  739.    are currently live.  Their bits are also flagged in allocnos_live.  */
  740.  
  741. static void
  742. record_conflicts (allocno_vec, len)
  743.      register short *allocno_vec;
  744.      register int len;
  745. {
  746.   register int allocno;
  747.   register int j;
  748.   register int ialloc_prod;
  749.  
  750.   while (--len >= 0)
  751.     {
  752.       allocno = allocno_vec[len];
  753.       ialloc_prod = allocno * allocno_row_words;
  754.       IOR_HARD_REG_SET (hard_reg_conflicts[allocno], hard_regs_live);
  755.       for (j = allocno_row_words - 1; j >= 0; j--)
  756.     conflicts[ialloc_prod + j] |= allocnos_live[j];
  757.     }
  758. }
  759.  
  760. /* Handle the case where REG is set by the insn being scanned,
  761.    during the forward scan to accumulate conflicts.
  762.    Store a 1 in regs_live or allocnos_live for this register, record how many
  763.    consecutive hardware registers it actually needs,
  764.    and record a conflict with all other registers already live.
  765.  
  766.    Note that even if REG does not remain alive after this insn,
  767.    we must mark it here as live, to ensure a conflict between
  768.    REG and any other regs set in this insn that really do live.
  769.    This is because those other regs could be considered after this.  */
  770.  
  771. static void
  772. mark_reg_store (reg)
  773.      rtx reg;
  774. {
  775.   register int regno = REGNO (reg);
  776.  
  777.   if (reg_renumber[regno] >= 0)
  778.     regno = reg_renumber[regno];
  779.  
  780.   /* Either this is one of the max_allocno pseudo regs not allocated,
  781.      or it is or has a hardware reg.  First handle the pseudo-regs.  */
  782.   if (regno >= FIRST_PSEUDO_REGISTER)
  783.     {
  784.       if (reg_allocno[regno] >= 0)
  785.     {
  786.       SET_ALLOCNO_LIVE (reg_allocno[regno]);
  787.       record_one_conflict (regno);
  788.     }
  789.     }
  790.   /* Handle hardware regs (and pseudos allocated to hard regs).  */
  791.   else if (! fixed_regs[regno])
  792.     {
  793.       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  794.       while (regno < last)
  795.     {
  796.       record_one_conflict (regno);
  797.       SET_HARD_REG_BIT (hard_regs_live, regno);
  798.       regno++;
  799.     }
  800.     }
  801. }
  802.  
  803. /* Mark REG as being dead (following the insn being scanned now).
  804.    Store a 0 in regs_live or allocnos_live for this register.  */
  805.  
  806. static void
  807. mark_reg_death (reg)
  808.      rtx reg;
  809. {
  810.   register int regno = REGNO (reg);
  811.  
  812.   /* For pseudo reg, see if it has been assigned a hardware reg.  */
  813.   if (reg_renumber[regno] >= 0)
  814.     regno = reg_renumber[regno];
  815.  
  816.   /* Either this is one of the max_allocno pseudo regs not allocated,
  817.      or it is a hardware reg.  First handle the pseudo-regs.  */
  818.   if (regno >= FIRST_PSEUDO_REGISTER)
  819.     {
  820.       if (reg_allocno[regno] >= 0)
  821.     CLEAR_ALLOCNO_LIVE (reg_allocno[regno]);
  822.     }
  823.   /* Handle hardware regs (and pseudos allocated to hard regs).  */
  824.   else if (! fixed_regs[regno])
  825.     {
  826.       /* Pseudo regs already assigned hardware regs are treated
  827.      almost the same as explicit hardware regs.  */
  828.       register int last = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
  829.       while (regno < last)
  830.     {
  831.       CLEAR_HARD_REG_BIT (hard_regs_live, regno);
  832.       regno++;
  833.     }
  834.     }
  835. }
  836.  
  837. /* Mark hard reg REGNO as currently live, assuming machine mode MODE
  838.    for the value stored in it.  MODE determines how many consecutive
  839.    registers are actually in use.  Do not record conflicts;
  840.    it is assumed that the caller will do that.  */
  841.  
  842. static void
  843. mark_reg_live_nc (regno, mode)
  844.      register int regno;
  845.      enum machine_mode mode;
  846. {
  847.   register int last = regno + HARD_REGNO_NREGS (regno, mode);
  848.   while (regno < last)
  849.     {
  850.       SET_HARD_REG_BIT (hard_regs_live, regno);
  851.       regno++;
  852.     }
  853. }
  854.  
  855. /* Print debugging trace information if -greg switch is given,
  856.    showing the information on which the allocation decisions are based.  */
  857.  
  858. static void
  859. dump_conflicts (file)
  860.      FILE *file;
  861. {
  862.   register int i;
  863.   fprintf (file, ";; %d regs to allocate:", max_allocno);
  864.   for (i = 0; i < max_allocno; i++)
  865.     {
  866.       fprintf (file, " %d", allocno_reg[allocno_order[i]]);
  867.       if (allocno_size[allocno_order[i]] != 1)
  868.     fprintf (file, " (%d)", allocno_size[allocno_order[i]]);
  869.     }
  870.   fprintf (file, "\n");
  871.  
  872.   for (i = 0; i < max_allocno; i++)
  873.     {
  874.       register int j;
  875.       fprintf (file, ";; %d conflicts:", allocno_reg[i]);
  876.       for (j = 0; j < max_allocno; j++)
  877.     if (CONFLICTP (i, j) || CONFLICTP (j, i))
  878.       fprintf (file, " %d", allocno_reg[j]);
  879.       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
  880.     if (TEST_HARD_REG_BIT (hard_reg_conflicts[i], j))
  881.       fprintf (file, " %d", j);
  882.       fprintf (file, "\n");
  883.     }
  884.   fprintf (file, "\n");
  885. }
  886.  
  887. void
  888. dump_global_regs (file)
  889.      FILE *file;
  890. {
  891.   register int i;
  892.  
  893.   fprintf (file, ";; Register dispositions:");
  894.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  895.     {
  896.       if (reg_renumber[i] >= 0)
  897.     fprintf (file, " %d in %d ", i, reg_renumber[i]);
  898.     }
  899.  
  900.   fprintf (file, "\n\n;; Hard regs used: ");
  901.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  902.     if (regs_ever_live[i])
  903.       fprintf (file, " %d", i);
  904.   fprintf (file, "\n\n");
  905. }
  906.